(function(){ 

	var classFileSelect = "dd-file",
		classImagePreview = "dd-image",
		classDisplay = "dd-display",
		classEdit = "dd-edit",
		classProductPic = "imageProduct",
		classPropertyPic= "dd-property";
	
	//click image to upload and preview new image
	$(document).on("click","." + classProductPic + ",." + classImagePreview + ",." + classPropertyPic, function(e){
		var handle = e.target;	
		
		var test = $(handle).next("." + classFileSelect);
		//transfer image click to file input
		$(handle).next("." + classFileSelect).click(); 

		//Load image into <img>
		$(handle).siblings("." + classFileSelect).on("change", function(){
			var objUrl = getObjectURL(this.files[0]);
			console.log("objUrl = "+objUrl) ;
			if (objUrl) {
				$(handle).attr("src", objUrl) ;
			}
		});

		//Create a url able to read the file
		function getObjectURL(file) {
			var url = null ; 
			if (window.createObjectURL!=undefined) { // basic
				url = window.createObjectURL(file) ;
			} else if (window.URL!=undefined) { // mozilla(firefox)
				url = window.URL.createObjectURL(file) ;
			} else if (window.webkitURL!=undefined) { // webkit or chrome
				url = window.webkitURL.createObjectURL(file) ;
			}
			return url ;
		};		
	});

	//double click text to edit it

	$(document).on("click","." + classDisplay, function(e) {
		// this.readOnly = false;
		this.className = this.className.replace(classDisplay, classEdit);
	});

	$(document).on("keyup","." + classDisplay, function(){ 
		var maxchar=50; 

		iCount = this.value.replace(/[^\u0000-\u00ff]/g,"aa").length; 
		if(iCount<=maxchar) 
		{
			this.size=iCount-2;
		}
		else alert("请不要超过"+maxchar);
	});

	$(document).on("blur", "." + classEdit, function() {
		// this.readOnly = true;
		this.className = this.className.replace(classEdit, classDisplay);
	});
})();